package com.example.demo.innoket.generate;

import com.example.demo.innoket.util.*;
import com.example.demo.innoket.vo.Table;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * java 代码模板
 * @author zhangLi
 */
public class JavaGenerate implements Generate {
	FileType javaFileType = null;

	public JavaGenerate(FileType javaFileType) {
		this.javaFileType = javaFileType;
	}

	@SuppressWarnings("static-access")
	public void generate(Table table, GeneratorConfig.ModulePath path) throws Exception {
		if (javaFileType == null)
			throw new Exception(JavaGenerate.class.getName() + ": JavaFileType 为null");
		/* get the Template */
//		System.out.println("Resources.TEMPLATE_PATH :" + Resources.TEMPLATE_PATH);
//		System.out.println("javaFileType.getTemplate() :" + javaFileType.getTemplate());

		Template t = new VelocityUtil().getTemplate(Resources.TEMPLATE_PATH+"/"+javaFileType.getTemplate());
		/* create a context and& nbsp;add data */
		VelocityContext context = new VelocityContext();
		String daoPackagePath = path.daoPath.substring(path.daoPath.indexOf("com")).replace("\\",".");
		String baseMapperPath = daoPackagePath.replace(".dao","");
		System.out.println("daoPackagePath : "+daoPackagePath);
		//String mapperPath = path.mapperPath.substring(path.mapperPath.indexOf("resources")).replace("\\",".");
		String mapperPath = path.mapperPath.substring(path.mapperPath.indexOf("com")).replace("\\",".");
		String entityPath = daoPackagePath.replace("dao","pojo");
		String modelPath = daoPackagePath.replace(".dao","");
		String expandPath = daoPackagePath.replace("dao", "expand");
		String servicePath = path.daoPath.replace("dao", "service").substring(path.daoPath.indexOf("com")).replace("\\",".");
		String serviceImplPath = servicePath.replace("\\",".");
		String controllerPath = path.daoPath.replace("dao", "web.controller").substring(path.daoPath.indexOf("com")).replace("\\",".");
		String apiControllerPath = path.daoPath.replace("dao", "app.controller").substring(path.daoPath.indexOf("com")).replace("\\",".");
		String mqConfigPath =  path.daoPath.replace("dao", "mq.config").substring(path.daoPath.indexOf("com")).replace("\\",".");
		String mqReceiverPath =  path.daoPath.replace("dao", "mq.receiver").substring(path.daoPath.indexOf("com")).replace("\\",".");
		context.put("mqConfigPackage",mqConfigPath);
		context.put("mqReceiverPackage",mqReceiverPath);
		context.put("expandPackage", expandPath);
		context.put("mapperPackage", mapperPath);
		context.put("entityPackage", entityPath);
		context.put("modelPackage", modelPath);
		context.put("daoPackagePath", daoPackagePath);
		context.put("baseMapperPath", baseMapperPath);
		context.put("servicePackage", servicePath);
		context.put("serviceImplPackage", serviceImplPath);
		context.put("controllerPackage", controllerPath);
		context.put("appControllerPackage", apiControllerPath);
		context.put("excludeCol",GeneratorConfig.EXCLUDE_COL);
		//获取实例对象名
		String tableName = table.getTableName().toLowerCase();
		StringBuilder sb = new StringBuilder();
		String[] arrs = tableName.split("_");
		if(arrs!=null && arrs.length>0){
			int len = arrs.length;
			if (len > 1) {
				for (int i = 1; i < len; i++) {
					String s = arrs[i];
					sb.append(Character.toUpperCase(s.charAt(0)));
					sb.append(s.substring(1));
				}
			}
		}
		//前缀
		String model = arrs[0].toLowerCase();
//		String model = "common_finance";
//		String src = "src//main//java";
		String entityInstance= ConvertHandler.getEntityInstance(tableName);

		String className = ConvertHandler.table2clazzName(tableName);

		context.put("tableName", tableName);
		context.put("entityName", className);
		context.put("lowerEntityName",Underline2Camel.toLowerCaseFirstOne(className));
		context.put("upperEntityName",Underline2Camel.camel2Underline(className).toUpperCase());//camel2Underline
		context.put("entityInstance", entityInstance);
		//创建 jspFileName 
		String jspFile = new Underline2Camel().camel2Underline(entityInstance).toLowerCase();
		String apiName = jspFile.replaceAll("_", "/");
		context.put("jspFileName", jspFile);
		context.put("apiName", apiName);
		context.put("tableName", table.getTableName().toLowerCase());
		context.put("columns", table.getColumns());

		context.put("requestMapping", Resources.REQUEST_MAPPING);
		context.put("moudle", entityInstance);//模块名
		context.put("model", model);//前缀
		context.put("author", Resources.AUTHOR);//作者
		context.put("idStratage", GeneratorConfig.ID_GENERATE_STRATGE);//作者

		context.put("dateType", ConvertHandler.getDateType(table.getColumns()));//判断时间类型
		context.put("timestampType", ConvertHandler.getTimestampType(table.getColumns()));//判断时间类型
		context.put("bigDecimal", ConvertHandler.getBigDecimalType(table.getColumns()));//判断浮点数类型

		//获取系统时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		context.put("createfiletime", sdf.format(new Date()));
    	
		/* now render the template into a StringWriter */
		StringWriter writer = new StringWriter();
		t.merge(context, writer);
//		if(FileType.DAO.equals(javaFileType)){//dao
//			FileUtil.create(Resources.BASE_PACKAGE + path.getPath(javaFileType) , className + javaFileType.getFileNameExtension(), writer.toString());
//		}
//		if(FileType.MAPPER.equals(javaFileType)){//mapper
//			FileUtil.create(Resources.BASE_PACKAGE + path.getPath(javaFileType) , className + javaFileType.getFileNameExtension(), writer.toString());
//		}
//		if(FileType.CONTROLLER.equals(javaFileType)){//controller
//			FileUtil.create(Resources.BASE_PACKAGE + path.getPath(javaFileType) , className + javaFileType.getFileNameExtension(), writer.toString());
//		}
//		if(FileType.ENTITY.equals(javaFileType)){//vo
//			//E:\work\workspace\common-finance\src\main\java\com\common\finance\vo
//			//E:\work\workspace\common_finance\src\main\java\com\common\finance\vo/TbFinance.java
////			System.out.println("Resources.BASE_PACKAGE = "+Resources.BASE_PACKAGE);//E:\work\workspace\
////			System.out.println("model = "+model);//common_finance
////			System.out.println("className = "+className);//FinanceLog
////			System.out.println("vo = "+FileType.ENTITY.getType());//vo
////			System.out.println("src = "+src);//src/main/java//finance.common.
////			System.out.println("path.getPath(javaFileType) = "+path.getPath(javaFileType));//.java
//
//			System.out.println("javaFileType.getFileNameExtension() = "+javaFileType.getFileNameExtension());
			FileUtil.create(Resources.BASE_PACKAGE + path.getPath(javaFileType) , className + javaFileType.getFileNameExtension(), writer.toString());
////			FileUtil.create(Resources.BASE_PACKAGE  + "/" + model + "/"+ src + path.getPath(javaFileType), className + javaFileType.getFileNameExtension(), writer.toString());
//		}
		System.out.println();
		if(FileType.SERVICE.equals(javaFileType) || FileType.SERVICE_IMPL.equals(javaFileType)){
//			FileUtil.create(Resources.BASE_PACKAGE  + path.getPath(javaFileType) + "/" + model, className + javaFileType.getFileNameExtension(), writer.toString());
			FileUtil.create(Resources.BASE_PACKAGE + path.getPath(javaFileType) , className + javaFileType.getFileNameExtension(), writer.toString());
		}else{
			FileUtil.create(Resources.BASE_PACKAGE + path.getPath(javaFileType) , className + javaFileType.getFileNameExtension(), writer.toString());

		}
	}

	public void generate(List<Table> tables,GeneratorConfig.ModulePath path) throws Exception {

	}

}
